package com.sea.physical.service.impl;


import com.codecrane.core.controller.UploaderController;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.operate.entity.Related;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedService;
import com.sea.operate.service.RelatedSkuService;
import com.sea.physical.entity.CreatePackingSku;
import com.sea.physical.entity.CreateSku;
import com.sea.physical.mapper.CreatePackingSkuMapper;
import com.sea.physical.mapper.CreateSkuMapper;
import com.sea.physical.service.CreatePackingSkuService;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.mapper.ChannelMapper;
import com.sea.product.service.ProductService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Service
public class CreatePackingSkuServiceImpl implements CreatePackingSkuService {

    @Autowired
    private CreatePackingSkuMapper createPackingSkuMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private CreateSkuService createSkuService;
    @Autowired
    private CreatePlanService createPlanService;




    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, Integer createId) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream,fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CreatePackingSku> skuList = BatchImportUtils.iteratorForCreatePackingSku(sheet);
            if(null==skuList||skuList.size()==0){
                throw new Exception("请至少新增一条明细！");
            }else{
                for(CreatePackingSku createPackingSku:skuList){
                    Product product = productService.findBySku(createPackingSku.getCreateSku());
                    if(null==product){
                        throw new RuntimeException("SKU【"+createPackingSku.getCreateSku()+"】不存在!");
                    }
                    Channel channel = channelMapper.findByChannelName(createPackingSku.getCreateChannelName());
                    if(null==channel){
                        throw new RuntimeException("渠道【"+createPackingSku.getCreateChannelName()+"】不存在!");
                    }
                    createPackingSku.setCreateId(createId);
                    save(createPackingSku);
                }
                updatePercent(createId);
                findTotalByCreateId(createId);
            }
        }
    }






    /**
     * 更新比例
     * @param createId
     */
    public void updatePercent(Integer createId){
        CreatePackingSku total = findNumberTotal(createId);
        List<CreatePackingSku> list = findByCreateId(createId);
        for(CreatePackingSku createPackingSku:list){
            createPackingSku.setCreatePercent((double)createPackingSku.getCreateNumber()/(double)total.getCreateNumber());
            createPackingSku.setCreateWeightTotal(createPackingSku.getCreatePercent()*(double)total.getCreateBoxWeight());
            createPackingSku.setCreateKgTotal(createPackingSku.getCreatePercent()*(double)total.getCreateBoxKg());
            createPackingSku.setCreateVolumeTotal(createPackingSku.getCreatePercent()*(double)total.getCreateBoxVolume());
            usave(createPackingSku);
        }

    }

    @Override
    public Integer save(CreatePackingSku createPackingSku) {

        return createPackingSkuMapper.insert(createPackingSku);
    }

    @Override
    public CreatePackingSku findNumberTotal(Integer createId) {
        return createPackingSkuMapper.findNumberTotal(createId);
    }

    @Override
    public List<CreatePackingSku> findByCreateId(Integer createId) {
        return createPackingSkuMapper.findByCreateId(createId);
    }

    @Override
    public Integer usave(CreatePackingSku createPackingSku) {
        return createPackingSkuMapper.update(createPackingSku);
    }

    @Override
    public List<CreatePackingSku> findDetailByCreateId(Integer createId) {
        return createPackingSkuMapper.findDetailByCreateId(createId);
    }

    @Override
    public List<CreatePackingSku> findTotalByCreateId(Integer createId) {
        List<CreatePackingSku> list = createPackingSkuMapper.findTotalByCreateId(createId);
        for(CreatePackingSku createPackingSku:list){
            CreateSku createSku=new CreateSku();
            BeanUtils.copyProperties(createPackingSku,createSku);
            createSku.setCreatePackage(createPackingSku.getCreateNumber());
            createSku.setCreateWeight(createPackingSku.getCreateWeightTotal());
            createSku.setCreateVolume(createPackingSku.getCreateVolumeTotal());
            createSku.setCreateKgTotal(createPackingSku.getCreateKgTotal());
            createSkuService.save(createSku);
        }
        createPlanService.updateTotal(createId);

        return createPackingSkuMapper.findTotalByCreateId(createId);
    }

    @Override
    public Integer findBoxNumberByCreateId(Integer createId) {
        return createPackingSkuMapper.findBoxNumberByCreateId(createId);
    }


}
